package org.apache.lucene.distributed;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;

/**
 * Finds Lucene core Lucene subclasses
 *
 */
public class GetLuceneClasses {
  Set<String> rootNames;
  static Set<String> defaultRootNames;
  static {
    defaultRootNames = new HashSet<String>();
    /**
    defaultRootNames.add("org.apache.lucene.search.Query");
    defaultRootNames.add("org.apache.lucene.search.Filter");
    defaultRootNames.add("org.apache.lucene.analysis.Analyzer");
    defaultRootNames.add("org.apache.lucene.document.Document");
    defaultRootNames.add("org.apache.lucene.document.Fieldable");
    defaultRootNames.add("org.apache.lucene.search.Similarity");
    defaultRootNames.add("org.apache.lucene.search.Weight");
    **/
    defaultRootNames.add("java.lang.Object");
  }

  public static void main(String[] args) throws Exception {
    // GetLuceneClasses getLuceneClasses = new
    // GetLuceneClasses(defaultRootNames);
    // System.out.println(getLuceneClasses.get() + "");
    GenerateLuceneClassExclude e = new GenerateLuceneClassExclude(new File(
        "G:\\oceangoogle\\trunk\\distributedlucene\\src\\org\\apache\\lucene\\distributed\\LuceneClasses.java"));
    e.generate();
    System.out.println("finished");
  }

  public static class GenerateLuceneClassExclude {
    File file;

    public GenerateLuceneClassExclude(File file) {
      this.file = file;
    }

    public void generate() throws Exception {
      StringBuilder b = new StringBuilder();
      b.append("package org.apache.lucene.distributed;\n\n");
      b.append("import java.util.HashSet;\n");
      b.append("import java.util.Set;\n\n");
      b.append("public class LuceneClasses {\n");
      b.append("  public static Set<String> classes = new HashSet<String>();\n\n");
      b.append("  static {\n");
      GetLuceneClasses glc = new GetLuceneClasses(defaultRootNames);
      Set<String> set = glc.get();
      for (String name : set) {
        String s = name.substring(name.lastIndexOf('.')+1, name.length());
        //System.out.println(s);
        if (s.startsWith("Test")) continue;
        b.append("    classes.add(\"" + name + "\");\n");
      }
      b.append("  }");
      b.append("}");

      FileUtils.writeStringToFile(file, b.toString());
    }
  }

  public GetLuceneClasses(Set<String> rootNames) {
    this.rootNames = rootNames;
  }

  public Set<String> get() throws Exception {
    Set<String> names = new HashSet<String>();
    ResolverUtil resolverUtil = new ResolverUtil();
    for (String rootName : rootNames) {
      Class cl = Class.forName(rootName);
      resolverUtil.findImplementations(cl, "org.apache.lucene");
    }
    Set<Class> classes = resolverUtil.getClasses();
    for (Class c : classes) {
      names.add(c.getName());
    }
    return names;
  }
}
